/**
 * jQuery plugins
 */
(function($)
{
	"use strict";
	/**
	 * Add item to table of contents
	 * @param {Array.<Collection>|Array.<Set>} list Collection or set array
	 * @param {Function(String)} activate Method taking string ID executed when item is clicked
	 * @param {Function(String, String)} describe Method taking description and title
	 * @param {Boolean} [clickable = false] Whether item can be clicked directly or only hovered
	 * @return {jQuery}
	 */
	$.fn.addListItems = function(list, activate, describe, clickable)
	{
		var item = null;
		var $li = null;

		return this.each(function()
		{
			for (var i = 0; i < list.length; i++)
			{
				item = list[i];
				$li = $('<li>')
					.append(item.title)
					.append('<div class="fade"></div>')
					.appendIcon('chevron-right')
					.attr('id', item.id)
					.data('description', item.description)
					.hover(function()
					{
						if (!clickable) { activate(this.id); }
						describe($(this).data('description'), $(this).data('title'));
					},
					function() { describe(); }
				);

				if (clickable)
				{
					$li.data('title', item.title).click(function() { activate(this.id); });
				}
				$(this).append($li);
			}
		});
	};

	/**
	 * @param {String} name
	 * @param {Boolean} [white = false]
	 * @return {*|Object|!jQuery|Object|Object|!jQuery|Object}
	 * @see http://twitter.github.com/bootstrap/base-css.html#icons
	 */
	$.fn.appendIcon = function(name, white)
	{
		var className = 'icon-' + name;
		if (white) { className += ' icon-white'; }

		return this.each(function()
		{
			$(this).append($('<i>').addClass(className));
		});
	};

	/**
	 * Set opacity to a value and then a second value
	 * @param {int} duration Time in milliseconds to change and restore opacity
	 * @param {Number} firstOpacity Opacity to bounce to
	 * @param {Number} secondOpacity Opacity to return to
	 * @return {jQuery}
	 */
	$.fn.bounceOpacity = function(duration, firstOpacity, secondOpacity)
	{
		return this.each(function()
		{
			var n = $(this);
			n.fadeTo(duration / 2, firstOpacity, function() { n.fadeTo(duration / 2, secondOpacity); })
		});
	}
})(jQuery);